package com.wss.lsl.lucene.demo.shipin.advanced.query;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.util.Version;

/**
 * 自定义parse。1、考虑到性能；2、考虑某些需求需要扩展
 * 
 * @author lenovo
 * 
 */
public class CustomParse extends QueryParser {

	public CustomParse(Version matchVersion, String f, Analyzer a) {
		super(matchVersion, f, a);
	}

	@Override
	protected org.apache.lucene.search.Query getWildcardQuery(String field,
			String termStr) throws ParseException {

		throw new ParseException("由于性能原因，系统禁用通配符查询，请输入更精确的信息进行查询!");
	}

	@Override
	protected org.apache.lucene.search.Query getFuzzyQuery(String field,
			String termStr, float minSimilarity) throws ParseException {

		throw new ParseException("由于性能原因，系统禁用模糊查询，请输入更精确的信息进行查询!");
	}

	@Override
	protected org.apache.lucene.search.Query getRangeQuery(String field,
			String part1, String part2, boolean inclusive)
			throws ParseException {
		if (field.equals("size")) {
			return NumericRangeQuery.newLongRange(field, Long.valueOf(part1),
					Long.valueOf(part2), inclusive, inclusive);
		}
		// FIXME
		// 对日期类型，part1/part2(日期的字符串格式)转化为Date，再转化为Long，返回NumericRangeQuery
		return super.getRangeQuery(field, part1, part2, inclusive);
	}

}
